// pages/person/index.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: "",
    avatarUrl: "",
    gender: "",
    province: "",
    city: "",
    country: "",
    iszan: true,

    items: []
  },

  onLoad: function (options) {

    //获取storage中存放的用户信息
    var userInfo = wx.getStorageSync('userinfo');
    var that = this;
    that.setData({
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      gender: userInfo.gender,
      province: userInfo.province,
      city: userInfo.city,
      country: userInfo.country
    });
    var question_sum;
    const openid = wx.getStorageSync('openid');
    wx.request({
      url: 'http://localhost:8080/question_sum',
      method: "GET",
      success: (res) => {
        question_sum = res.data;
        // this.setData({
        // question_sum:res.data
        // })
        console.log("resdata:", res.data)
        for (var i = 1, len = question_sum; i <= len; i++) {
          console.log("len:",len);
          wx.request({
            url: 'http://localhost:8080/get_question_by_question_id',
            method: 'GET',
            // dataType: 'json',
            data: {
              question_id: i
            },
            success: (res) => {
              console.log(res.data);
              var str = res.data.question_content;
              if (str.length > 40) {
                str = str.slice(0, 35);
              }
              else if(str.length<=17){
                str=str+'\n\n';
                console.log(str);
              }

              var item = {
                user_id: res.data.user_id,
                usrName: '',
                usrPhoto: '',
                star_show: true,
                like_show: true,
                title: str,
                desc: res.data.question_content,
                qid:res.data.question_id,
                award: res.data.award,
                flag: res.data.label
              }
              console.log("item:",item)
              var list = that.data.items;
              list.push(item),
                  console.log(item)
                  this.setData({
                    items: list
                  })
                  console.log(this.data.items)
              // list.push(item);
              // wx.request({
              //   url: 'http://localhost:8080/get_like_star',
              //   method:'GET',
              //   data:{
              //     user_id:openid,
              //     question_id:res.data.question_id,
              //   },
              //   success:function(res){
              //     console.log("res",res.data)
              //     item.star_show=res.data.star,
              //     item.like_show=res.data.like_count,
                  
              //   }
              // })

            }
          })
        }
      }
    })

    //获取问题列表


  },


  starthis: function (e) {
    var that = this;
    console.log("~收藏");
    var index = e.currentTarget.dataset.curindex;
    console.log(index);
    const openid = wx.getStorageSync('openid');
    if (that.data.items[index]) {
      that.data.items[index].star_show = !that.data.items[index].star_show;
      console.log(that.data.items[index].star_show);

      //修改表中数据
      wx.request({
        url: 'http://localhost:8080/can_get_like_star',
        method: 'GET',
        data: {
          user_id: openid,
          question_id: that.data.items[index].qid,
        },
        success: function (res) {
          console.log(res)
          console.log("res data:", res.data)
          if (res.data == true) {
            wx.request({
              url: 'http://localhost:8080/update_like_star',
              method: 'POST',
              data: {
                user_id: openid,
                question_id: that.data.items[index].qid,
                like_count: that.data.items[index].like_show,
                star: that.data.items[index].star_show
              },
              success: function (res) {
                console.log(res.data)
              }
            })
          } else {
            wx.request({
              url: 'http://localhost:8080/like_star',
              method: 'POST',
              data: {
                user_id: openid,
                question_id: that.data.items[index].qid,
                like_count: that.data.items[index].like_show,
                star: that.data.items[index].star_show
              }
            })
          }
        }
      })

      this.setData({
        items: that.data.items,
      });
    }

  },
  likethis: function (e) {
    console.log("点赞");
    var that = this;
    console.log("~收藏");
    var index = e.currentTarget.dataset.curindex;
    console.log(index);
    const openid = wx.getStorageSync('openid');
    if (that.data.items[index]) {
      that.data.items[index].like_show = !that.data.items[index].like_show;
      console.log(that.data.items[index].like_show);
      wx.request({
        url: 'http://localhost:8080/can_get_like_star',
        method: 'GET',
        data: {
          user_id: openid,
          question_id: that.data.items[index].qid,
        },
        success: function (res) {
          console.log(res)
          if (res.data == true) {
            wx.request({
              url: 'http://localhost:8080/update_like_star',
              method: 'POST',
              data: {
                user_id: openid,
                question_id: that.data.items[index].qid,
                like_count: that.data.items[index].like_show,
                star: that.data.items[index].star_show
              },
              success: function (res) {
                console.log(res.data)
              }
            })
          } else {
            wx.request({
              url: 'http://localhost:8080/like_star',
              method: 'POST',
              data: {
                user_id: openid,
                question_id: that.data.items[index].qid,
                like_count: that.data.items[index].like_show,
                star: that.data.items[index].star_show
              }
            })
          }
        }
      })

      this.setData({
        items: that.data.items,
      });
    }
  },

  gotoPost() {
    wx.navigateTo({
      url: '/pages/post/post',
    })
  },

  gotoQUE(e) {
    let index = e.currentTarget.dataset.curindex;
    var that = this;
    console.log("index:", index);
    console.log("qid:", that.data.items[index].qid);
    wx.navigateTo({
      url: '/pages/que_detail/que_detail?qid=' + that.data.items[index].qid,
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: that.data.items[index]
        })
      }
    })
  }

})